package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.PostBean;
import com.project.bean.ReplayBean;
import com.project.bean.UserBean;
import com.project.mapper.IPostMapper;
import com.project.mapper.IReplayMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IPostService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class PostServiceImpl implements IPostService {
    @Autowired
    private IPostMapper postMapper;
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IReplayMapper replayMapper;
    @Override
    public IPage<PostBean> cutByItem(Integer pageNO, Integer channelId, String title, String username, LocalDate startDate, LocalDate endDate) {
        return postMapper.cutByItem(new Page(pageNO,4),channelId,title,username,startDate,endDate);
    }

    @Override
    public PostBean findById(Integer id) {
        PostBean post = postMapper.selectById(id);
        //查询主贴的用户
        UserBean user = userMapper.selectById(post.getUserId());
        post.setUser(user);

        //查询主贴的回帖集合
        List<ReplayBean> replayList = replayMapper.findByPost(post.getId());
        post.setReplayList(replayList);
        return post;
    }

    @Override
    public void add(PostBean post) {
        postMapper.insert(post);
    }
}
